<?php

namespace app\admin\command;

use app\admin\model\BuildingModel;
use app\admin\model\BuildingRoomModel;
use app\admin\model\UserConfigModel;
use app\common\library\Email;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class SendEmail extends Command
{

    protected function configure()
    {
        $this
            ->setName('send_house_email')
            ->setDescription('发送房源信息给房东');
    }

    protected function execute(Input $input, Output $output)
    {
        $rows = UserConfigModel::where([
            'field' => 'backup_email',
            'value' => ['neq', ''],
        ])->select();
        foreach ($rows as $row) {
            $backup_email = $row->value;
            $backup_time = (new UserConfigModel)->getUserConfig($row->user_id, 'general', 'backup_time');
            if (empty($backup_time)) {
                continue;
            }
            //1每天、2每周、3每月、4每年
            if ($backup_time == 2 && date('N') != 1) {//每周一
                continue;
            } elseif ($backup_time == 3 && date('d') != 1) {//每月1号
                continue;
            } elseif ($backup_time == 4 && date('m') != 1 && date('d') != 1) {
                continue;
            }
            $result = $this->backup($row['user_id'], $backup_email);
            $output->writeln(sprintf('用户ID：%s，邮箱：%s，发送%s', $row['user_id'], $backup_email, $result ? '成功' : '失败'));

        }

        $output->writeln('执行完成');
    }


    private function backup($user_id, $backup_email)
    {
        $build_ids = BuildingModel::getUserBuildingIds($user_id);
        if (empty($build_ids)) {
            return false;
        }

        $room_ids = BuildingRoomModel::whereIn('build_id', $build_ids)->column('id');
        if (empty($room_ids)) {
            return false;
        }

        $model = new \app\admin\model\HouseModel;
        $list = $model->whereIn('room_id', $room_ids)
            //->where('is_finish', 0)
            ->select();

        $data = [];
        foreach ($list as $item) {
            $item->append(['house_type_text', 'payment_type_text', 'rent_type_text',
                'community_text', 'street_text', 'decoration_text']);
            $data[] = [
                $item['name'],
                $item['type_text'],
                $item['community_text'],
                $item['rent'],
                $item['area'],
                $item['house_type_text'],
                $item['floor'],
                $item['payment_type_text'],
                $item['face'],
                $item['decoration_text'],
                $item['water_rate'],
                $item['power_rate'],
                $item['address'],
                $model->getStatusList()[$item['status']] ?? '',
                $item['is_ax'] == 1 ? '是' : '否',
                $item['is_recommend'] == 1 ? '是' : '否',
                $item['visit_num'],
                $item['is_finish'] == 1 ? '是' : '否',
            ];
        }

        $title = [
            '名称',
            '类型',
            '社区',
            '租金',
            '面积',
            '户型',
            '楼层',
            '付款方式',
            '朝向',
            '装修',
            '水费',
            '电费',
            '位置',
            '状态',
            '安心房源',
            '推荐房源',
            '浏览量',
            '是否签约',
        ];
        $excel = export_excel($title, $data, '房源');

        if (!empty($excel['filePath'])) {
            $file_path = ROOT_PATH . 'public' . $excel['filePath'];
            $email = new Email();
            return $email->to($backup_email)
                ->subject('请查收你的房源信息')
                ->message('目前对外招租的房源信息，见附件。')
                ->attachment($file_path, '房源信息.xlsx')
                ->send();
        }
        return false;
    }


}
